package view.usuarios;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.Instructor;
import model.PersonalAdministrativo;
import model.Usuario;
import model.comunication.IInstructor;
import model.comunication.IPersonalAdministrativo;
import model.comunication.IUsuario;

public class AltaUsuario extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JFrame frmCrearUsuario;
	private JTextField textUserName;
	private JTextField textPassword;
	private IUsuario cmmUsuario;
	private JLabel lblPermisos;
	private JLabel lblPassword;
	private JLabel lblUserName;
	private JLabel lblMensajeError;
	private static PersonalAdministrativo personalLocal;// chantada me quede sin teoria
	private static Instructor instructorLocal;// chantada me quede sin teoria
	private IInstructor cmmInstructor;
	private IPersonalAdministrativo cmmPersonalAdministrativo;
	private JComboBox comboBoxPermisos;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AltaUsuario window = new AltaUsuario();
					window.frmCrearUsuario.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public AltaUsuario() {
		cmmUsuario = IUsuario.getInstance();
		cmmPersonalAdministrativo = IPersonalAdministrativo.getInstance();
		cmmInstructor = IInstructor.getInstance();
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmCrearUsuario = new JFrame();
		frmCrearUsuario.setResizable(false);
		frmCrearUsuario.setIconImage(Toolkit.getDefaultToolkit().getImage(AltaUsuario.class.getResource("/Icon_/User_128.png")));
		frmCrearUsuario.setVisible(true);
		frmCrearUsuario.setTitle("Crear Usuario");
		frmCrearUsuario.setBounds(100, 100, 419, 246);
		frmCrearUsuario.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frmCrearUsuario.getContentPane().setLayout(null);

		lblUserName = new JLabel("User Name:");
		lblUserName.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblUserName.setBounds(16, 25, 78, 14);
		frmCrearUsuario.getContentPane().add(lblUserName);

		lblPassword = new JLabel("Password:");
		lblPassword.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblPassword.setBounds(16, 65, 78, 14);
		frmCrearUsuario.getContentPane().add(lblPassword);

		textUserName = new JTextField();
		textUserName.setBounds(85, 23, 115, 20);
		frmCrearUsuario.getContentPane().add(textUserName);
		textUserName.setColumns(10);

		textPassword = new JTextField();
		textPassword.setBounds(85, 63, 117, 20);
		frmCrearUsuario.getContentPane().add(textPassword);
		textPassword.setColumns(10);

		lblPermisos = new JLabel("Permisos:");
		lblPermisos.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblPermisos.setBounds(16, 105, 59, 14);
		frmCrearUsuario.getContentPane().add(lblPermisos);

		comboBoxPermisos = new JComboBox();
		comboBoxPermisos.setModel(new DefaultComboBoxModel(new String[] { "-----------------------", "PersonalAdmin", "Instructor", "Supervisor" }));
		comboBoxPermisos.setBounds(85, 103, 117, 20);
		frmCrearUsuario.getContentPane().add(comboBoxPermisos);

		JButton btnGuardar = new JButton("Guardar");
		btnGuardar.setIcon(new ImageIcon(AltaUsuario.class.getResource("/Icon_/save_16.png")));
		btnGuardar.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnGuardar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// TODO agregar idUsuario al Empleado
				if (!textUserName.getText().equals("") && !textPassword.getText().equals("") && comboBoxPermisos.getSelectedIndex() != 0) {
					Usuario altaUser = new Usuario(textUserName.getText(), textPassword.getText(), comboBoxPermisos.getSelectedItem().toString());
					cmmUsuario.addUsuario(altaUser);

				} else {
					lblMensajeError.setVisible(true);
				}
			}
		});
		btnGuardar.setBounds(130, 170, 115, 30);
		frmCrearUsuario.getContentPane().add(btnGuardar);

		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.setIcon(new ImageIcon(AltaUsuario.class.getResource("/Icon_/exit.png")));
		btnCancelar.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				frmCrearUsuario.dispose();

			}
		});
		btnCancelar.setBounds(270, 170, 115, 30);
		frmCrearUsuario.getContentPane().add(btnCancelar);

		lblMensajeError = new JLabel("Los campos estan incompletos!!!!!! ");
		lblMensajeError.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblMensajeError.setVisible(false);
		lblMensajeError.setForeground(Color.RED);
		lblMensajeError.setBounds(16, 146, 200, 14);
		frmCrearUsuario.getContentPane().add(lblMensajeError);

		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon(AltaUsuario.class.getResource("/Icon_/login_icon_2.png")));
		label.setFont(new Font("Tahoma", Font.BOLD, 11));
		label.setBounds(253, 17, 117, 134);
		frmCrearUsuario.getContentPane().add(label);
	}
}
